package com.lx.fav.servlet;

import com.google.gson.Gson;
import com.lx.fav.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userinfo")
public class UserInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        Object userInfo = req.getSession().getAttribute("userInfo");
        Result ret;
        if (userInfo == null) {
            ret = new Result(0, "未登录", null);
        }else{
            ret = new Result(1, "已登录", userInfo);
        }
        String json = new Gson().toJson(ret);
        resp.getWriter().append(json);
    }
}
